package com.jinxinol.basic.system.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysDictionary;
import com.jinxinol.basic.system.domain.SysDirectorytype;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysDictionaryService;
import com.jinxinol.basic.system.service.ISysDirectorytypeService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.util.JSONGenerator;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author wangrong
 * @email qqq36985@163.com
 * @description 数据字典维护
 * @date 2012-9-5
 */
public class DictionaryAction extends BaseAction {
	private static final long serialVersionUID = -8004959989512432543L;
	private static final Log logger = ContextHelper.getLogger(DictionaryAction.class);
	//---------------------------------------------注入Service---------------------------------------------
	@Resource
	private ISysDirectorytypeService iSysDirectorytypeService;
	@Resource
	private ISysDictionaryService	iSysDictionaryService;
	
	
	//---------------------------------------------页面对象------------------------------------------------
	private SysUser user;
	private SysDictionary sysDictionary;
	private List<SysDirectorytype> dicTypeList;
	List<SysDictionary> dicList;
	private String dicTypeListJson;
	private boolean operResult=false;
	/**
	 * 进入数据字典维护页面
	 * @return
	 * @throws Exception 
	 */
	public String toDicPage() throws Exception{
		try{
			SysDirectorytype dtype = new SysDirectorytype();
			dtype.setIs_active(SysDirectorytype.IS_ACTIVE_YES);
			dicTypeList = iSysDirectorytypeService.find(dtype);
			dicTypeListJson = JSONGenerator.toJSON(dicTypeList);
		return "dic_page";
		}catch (Exception e) {
			e.printStackTrace();
			throw new  Exception("字典类型加载错误！\n详细日志："+e.getMessage());
		}
	}
	
	/**
	 * 按字典类型查询字典返回JSON字符串
	 * @throws Exception
	 */
	public String getDicByType()throws Exception{
		try{
			dicList = iSysDictionaryService.find(sysDictionary);
			
			return "dic_frame";
		}catch (Exception e) {
			e.printStackTrace();
			throw new  Exception("字典加载错误！\n详细日志："+e.getMessage());
		}
	}
	
	public String toDicEditPage()throws Exception{
		try {
			SysDirectorytype dtype = new SysDirectorytype();
			dtype.setIs_active(SysDirectorytype.IS_ACTIVE_YES);
			dicTypeList = iSysDirectorytypeService.find(dtype);
			if(sysDictionary!=null){
				sysDictionary = iSysDictionaryService.findById(sysDictionary.getId());
				return "dic_edit";
			}
		} catch (Exception e) {
			logger.error("跳转字典编辑页面出错!详细日志:"+e.getMessage());
			e.printStackTrace();
		}
		operResult=false;
		return "dic_add";
	}
	
	public String updateDic()throws Exception{
		try{
		sysDictionary.setUpdated(new Date());
		sysDictionary.setUpdatedby(sysUser.getId());
		iSysDictionaryService.update(sysDictionary);
		SysDictionary sysDictionaryWhere=new SysDictionary();
		sysDictionaryWhere.setTypeid(sysDictionary.getTypeid());
		sysDictionary=sysDictionaryWhere;
		operResult=true;
		}catch (Exception e) {
			logger.error("更新字典ID（"+sysDictionary.getId()+"）失败!详细日志："+e.getMessage());
		}
		return getDicByType();
	}
	
	public String addDic()throws Exception{
		try{
			sysDictionary.setCreated(new Date());
			sysDictionary.setCreatedby(sysUser.getId());
			iSysDictionaryService.insert(sysDictionary);
			if(sysDictionary.getId()!=null){
				operResult=true;
			}else{
				operResult=false;
			}
			return SUCCESS;
		}catch (Exception e) {
			logger.error("添加字典失败!详细日志："+e.getMessage());
		}
		return ERROR;
	}
	
	public String deleteDic()throws Exception{
		try{
			iSysDictionaryService.delete(sysDictionary);
			operResult=true;
			return getDicByType();
		}catch (Exception e) {
			logger.error("删除字典失败!详细日志："+e.getMessage());
		}
		return ERROR;
	}
	
	public SysUser getUser() {
		return user;
	}
	public void setUser(SysUser user) {
		this.user = user;
	}

	public SysDictionary getSysDictionary() {
		return sysDictionary;
	}

	public void setSysDictionary(SysDictionary sysDictionary) {
		this.sysDictionary = sysDictionary;
	}
	public List<SysDirectorytype> getDicTypeList() {
		return dicTypeList;
	}

	public void setDicTypeList(List<SysDirectorytype> dicTypeList) {
		this.dicTypeList = dicTypeList;
	}

	public String getDicTypeListJson() {
		return dicTypeListJson;
	}

	public void setDicTypeListJson(String dicTypeListJson) {
		this.dicTypeListJson = dicTypeListJson;
	}

	public List<SysDictionary> getDicList() {
		return dicList;
	}

	public void setDicList(List<SysDictionary> dicList) {
		this.dicList = dicList;
	}

	public boolean isOperResult() {
		return operResult;
	}

	public void setOperResult(boolean operResult) {
		this.operResult = operResult;
	}
}
